/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import static Dao.Conexao.getConnection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entity.Marca;
import model.entity.Modelo;

/**
 *
 * @author helton
 */
public class ModeloDao extends Conexao {

    public static Modelo insert(Modelo modelo) {
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Insert Into modelo (descmodelo,idmarca)"
                    + "Values(?,?)");
            pstmt.setObject(1, modelo.getDescModelo());
            pstmt.setObject(2, modelo.getIdMarca());
            pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            System.out.println("Erro: " + e);
        }
        return modelo;
    }

    public static Modelo update(Modelo modelo) {
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Update modelo Set descmodelo = ?,idmarca = ?"
                    + "Where idmodelo = ?");
            pstmt.setObject(1, modelo.getDescModelo());
            pstmt.setObject(2, modelo.getIdMarca());
            pstmt.setObject(3, modelo.getIdModelo());
            pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelo;
    }

    public static boolean remove(Modelo modelo) {
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Delete from modelo Where idmodelo =?");
            pstmt.setObject(1, modelo.getIdModelo());
            pstmt.executeUpdate();
            //
            pstmt.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Modelo find(int idmodelo) {
        Modelo modelo = null;
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Select * from modelo Where idmodelo =?");
            pstmt.setObject(1, idmodelo);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                modelo = new Modelo();
                modelo.setIdModelo(rs.getInt("idmodelo"));
                modelo.setDescModelo(rs.getString("descmodelo"));
            }
            //pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            System.out.println("Erro: "+e);
        }
        return modelo;
    }

    public static List<Modelo> getAll() {
        List<Modelo> listModelo = new ArrayList<Modelo>();
        Modelo modelo = null;
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "Select modelo.idmodelo,modelo.descmodelo,marca.descmarca "
                    + "from modelo,marca "
                    + "where modelo.idmarca = marca.idmarca");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                modelo = new Modelo();
                modelo.setIdModelo(rs.getInt("idmodelo"));
                modelo.setDescModelo(rs.getString("descmodelo"));
                modelo.setDescMarca(rs.getString("descmarca"));
                listModelo.add(modelo);
            }
            //pstmt.executeUpdate();
            //
            pstmt.close();
        } catch (Exception e) {
            System.out.println("Erro: " + e);
        }
        return listModelo;
    }
    
    
    
    public static List<Modelo> listaModelosporMarca(Integer idmarca) {
        Modelo modelo = null;
        MarcaDao marcadao = new MarcaDao();
        
        List<Modelo> listaModelos = new ArrayList<Modelo>();
        try {
            PreparedStatement pstmt = getConnection().prepareStatement(
                    "SELECT * FROM MODELO WHERE MARCA = ?");
            pstmt.setInt(1, idmarca);
            ResultSet rs = pstmt.executeQuery();
            Marca marca = new Marca();
            while (rs.next()) {
                marca = MarcaDao.find(rs.getInt("marca"));
                
                modelo = new Modelo();
                modelo.setIdModelo(rs.getInt("idmodelo"));
                modelo.setDescModelo(rs.getString("descricaoModelo"));
                modelo.setIdMarca(marca.getIdMarca());

                listaModelos.add(modelo);
                System.out.println(modelo);
            }
            pstmt.execute();

            pstmt.close();
        } catch (Exception e) {
            System.out.println("error" + e);
            e.printStackTrace();
        }

        return listaModelos;
    }
}
